<?php
// +---------------------------------------------------------------------+
// | OneBase    | [ WE CAN DO IT JUST THINK ]                            |
// +---------------------------------------------------------------------+
// | Licensed   | http://www.apache.org/licenses/LICENSE-2.0 )           |
// +---------------------------------------------------------------------+
// | Author     | Bigotry <3162875@qq.com>                               |
// +---------------------------------------------------------------------+
// | Repository | https://gitee.com/Bigotry/OneBase                      |
// +---------------------------------------------------------------------+

namespace app\common\model;


use think\Db;

/**
 * Game模型
 */
class PlanBetsLog extends ModelBase
{

    protected $insert = ['create_time','trade_no'];

    /**
     * AI流水号自动完成
     */
    public function setTradeNoAttr()
    {

        return 'PLBT'.date('Ymd').substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8).rand(1000,9999);
    }

    /**
     * 连用户表
     */
    public function user(){
        return $this->belongsTo('Member','user_id','id');
    }

    /**
     * 连彩种表
     */
    public function lottery(){
        return $this->belongsTo('Lottery','lottery_id','id');
    }

    /**
     * 连玩法表
     */
    public function play(){
        return $this->belongsTo('LotteryPlay','play_id','id');
    }
    /**
     * 获取状态信息内容
     */
    public function getStateHtmlAttr(){

        switch ($this->state){
            case 1:
                $html='<span class="el-tag el-tag--mini">未开奖</span>';
                break;
            case 2:
                $html='<span class="el-tag el-tag--success">中奖</span>';
                break;
            case 3:
                $html='<span class="el-tag el-tag--danger">未中奖</span>';
                break;

            case 4:
                $html='<span class="el-tag el-tag--danger">已撤单</span>';
                break;

        }
        return $html;
    }
    /**
     * 开奖号码
     */
    public function getRewardNumberAttr()
    {

        $info=LotteryResult::get(['lottery_id'=>$this->lottery_id,'round'=>$this->round]);
        if($info){
           return $info['number'];
        }
        return '';
    }
    /**
     * 时间自动完成
     */
    public function setCreateTimeAttr()
    {

        return TIME_NOW;
    }

    /**
     * 时间自动完成
     */
    public function getCreateTimeAttr($value)
    {

        return date('Y-m-d H:i:s',$value);
    }


    /**
     * 开奖结算
     */
    public function  new_settlement($res){
        $hit_number = $res['number'];
        $play=$this->play;
        $lottery=$this->lottery;
        $cate_symbol=$lottery->cate->symbol;
        if($this->is_old==1){
            include_once(ROOT_PATH.'extend/kaijiang/oldplay.php');
            $_zhushuyzclass = "Lib\\kaijiang\\oldplay";
            $_obj = new $_zhushuyzclass();
            $_fun=$play['symbol'];
            $arr=[
                'func'=>$_fun,
                'opencode'=>$hit_number,
                'tzcode'=>$this->number,
                'class2'=>$play->data['name']
            ];
            $iszjokcount=$_obj->check($arr);

        }else{
            include_once(ROOT_PATH.'extend/kaijiang/'.$cate_symbol.'.php');
            $_zhushuyzclass = "Lib\\kaijiang\\{$cate_symbol}";
            $_fun=$play['symbol'];
            $arr=[
                'playid'=>$_fun,
                'opencode'=>$hit_number,
                'tzcode'=>$this->number,
            ];
            $arrs[]=$arr;
            $_obj = new $_zhushuyzclass($arrs);
            $res=$_obj->check();

            $iszjokcount=$res[0]['zjcount'];
        }



        if($iszjokcount=='x5'){
            $reward_count=0;
        }else{
            $reward_count=$iszjokcount['zjcount'];
        }
        $zjcount=0;
        $programme=PlanUserProgramme::get($this->programme_id);
        $plan_programme=$programme->programme;
        if($reward_count>0){
            //中奖奖金
            if(strstr($this->odds,'|')){
                for($i=0;$i<count($iszjokcount['zjcount']);$i++){
                    if($reward_count[$i]>0){
                        $zjcount = $reward_count[$i];
                        break;
                    }
                }
            }else{
                $zjcount=$iszjokcount['zjcount'];
            }
            $reward_money=self::$cate_symbol([
                'mode'=>$this->odds,
                'iszjcount'=>$reward_count,
                'beishu'=>$this->mult,
                'iszjokcount'=>$iszjokcount,
                'amount'=>$this->money,
                'itemcount'=>$this->injection_number,
                'zjcount'=>$zjcount,
                'yjf'=>$this->univalent
            ]);

            $reward_money_total=$reward_money;
            $this->reward_money=$reward_money_total;

            if($reward_money_total<$this->money){
                $this->is_winner=2;
            }else{
                //赢

                $plan_programme->win_count+=1;

            }
            $this->state=2;

            $this->profit=$reward_money_total-$this->money;
            $this->reward_arr=implode(',',$iszjokcount['reward_arr']);

            //当前盈利
            $programme->current_win_money+=($reward_money_total-$this->money);
            //当前亏损
            $programme->current_lose_money-=($reward_money_total-$this->money);
        }else{

            //未中奖
            $this->state=3;
            $this->profit=-$this->money;
            $this->is_winner=2;
            //当前盈利
            $programme->current_win_money-=$this->money;
            //当前亏损
            $programme->current_lose_money+=$this->money;



        }
        $plan_programme->win_rate=format_money($plan_programme->win_count/$plan_programme->bets_count*100,2);
        $plan_programme->save();
        if($programme->is_stop_win==1){
            if($programme->current_win_money>=$programme->stop_win_money){
                $programme->state=4;
                $programme->has_end_time=TIME_NOW;
            }
        }
        if($programme->is_stop_lose==1){
            if($programme->current_lose_money>=$programme->stop_lose_money){
                $programme->state=4;
                $programme->has_end_time=TIME_NOW;
            }
        }
        if($programme['current_round']==$programme['round']){
            $programme->state=4;
            $programme->has_end_time=TIME_NOW;
        }
        $programme->save();
        $this->save();

    }


    protected function lhc($res){

        $okamount = 0;
        if(strstr($res["mode"],'|')){
            $mode = explode('|',$res["mode"]);
            for($i=0;$i<count($mode);$i++){
                if($res["iszjcount"][$i]>0){
                    $okamount +=($res['amount']/$res['itemcount'])*$mode[$i]*$res['beishu']*$res['iszjokcount'][$i];
                }
            }
        }else{
            $okamount = ($res['amount']/$res['itemcount'])*$res['mode']*$res['beishu']*$res['iszjokcount'];
        }

        return $okamount;
    }

    protected function ssc($res){
        $okamount =$res['mode']*$res['zjcount']*$res['beishu']*$res['yjf'];
        return $okamount;
    }
    protected function k3($res){
        $okamount = ($res['amount']/$res['itemcount'])*$res['mode']*$res['zjcount'];
        return $okamount;
    }
    protected function x5($res){
        $okamount = 0;
        if(strstr($res["mode"],'|')){
            $amount = explode('|',$res["mode"]);
            for($i=0;$i<count($amount);$i++){
                if($res['iszjokcount'][$i]!=0){
                    $okamount = $amount[$i]*$res['iszjokcount'][$i]*$res['beishu']*$res['yjf'];
                }
            }
        }else{

            $okamount =$res['mode']*$res['zjcount']*$res['beishu']*$res['yjf'];
        }
        return $okamount;
    }
    protected function kl10f($res){
        $okamount = 0;
        $rules = M('wanfa')->where(['typeid'=>$res['typeid'],'playid'=>$res['playid']])->find();
        if($rules){
            $defaultfandian = 0.13;
            $userinfo = [];
            $userinfo = M('member')->where(['id'=>$res['uid']])->find();
            $fandian = $userinfo['fandian'];
            if($rules['rate']>0){
                $amount = $res['mode']  *$res['yjf']*$res['beishu'];
                $okamount = $amount*$res['zjcount'];
            }else{
                $amount = (($rules['maxjj']/2) - ($defaultfandian-($fandian/100-$res['repoint']/100)) * $rules['totalzs'])*$res['yjf']*$res['beishu'];
                $okamount = $amount*$res['zjcount'];
            }
        }else{

        }
        return $okamount;
    }
    protected function pk10($res){
        $okamount =$res['mode']*$res['zjcount']*$res['beishu']*$res['yjf'];
        return $okamount;
    }
    protected function dpc($res){

        $okamount =$res['mode']*$res['zjcount']*$res['beishu']*$res['yjf'];
        return $okamount;
    }
    protected function keno($res){
        $okamount = 0;
        if(strstr($res["mode"],'|')){
            $amount = explode('|',$res["mode"]);
            for($i=0;$i<count($amount);$i++){
                if($res["iszjcount"][$i]>0){
                    $okamount = $okamount+($amount[$i]*$res["iszjcount"][$i])*$res['beishu']*$res['yjf'] ;
                    break;
                }
            }
        }else{
            $okamount =$res['mode']*$res['zjcount']*$res['beishu']*$res['yjf'];
        }
        return $okamount;

    }
}
